package com.fansaikj.erp;

import cn.hutool.db.DbUtil;
import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.json.JSONUtil;
import org.junit.Before;
import org.junit.Test;
import cn.hutool.db.Db;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import xyz.erupt.generator.model.GeneratorClass;
import xyz.erupt.generator.model.GeneratorField;
import xyz.erupt.jpa.dao.EruptDao;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class LoadGenTableInfo {

    // 定义待加载数据库表的名称前缀
    private static final String TABLE_PREFIX = "sys_";

    private DataSource dataSource;

    private Connection connection;

    @Before
    public void setUp() throws Exception
    {
        dataSource = DbUtil.getDs();
        connection = dataSource.getConnection();
    }

    @Test
    public void insertGeneratorClass() throws SQLException {
        List<String> tables = MetaUtil.getTables(dataSource);
        for (String tableName : tables) {
            if (tableName.startsWith(TABLE_PREFIX)) {
                Table table = MetaUtil.getTableMeta(dataSource, tableName);
                String[] split = table.getTableName().substring(TABLE_PREFIX.length()).split("_");
                String className = "";
                for (String s : split) {
                    className += s.substring(0, 1).toUpperCase() + s.substring(1);
                }
                Long tableId = SqlExecutor.executeForGeneratedKey(connection, "insert into e_generator_class(class_name,name,table_name) values (?,?,?)", className, className, table.getTableName());
                int sort = 0;
                for (Column column : table.getColumns()) {
                    boolean isQuery = false;
                    boolean isShow = true;
                    String type = "INPUT";
                    if (column.getName().equals("id") || column.getName().equals("create_time") || column.getName().equals("update_time")
                        || column.getName().equals("create_by") || column.getName().equals("update_by") || column.getName().equals("del_flag")) {
                        isShow = false;
                    }
                    if (column.getName().equals("remark")) {
                        type = "TEXTAREA";
                    }
                    switch (column.getTypeName()) {
                        case "INT":
                            type = "NUMBER";
                            break;
                        case "DATETIME":
                            type = "DATE";
                            break;
                    }
                    String columnName = "";
                    int i = 0;
                    for (String s : column.getName().split("_")) {
                        if (i == 0) {
                            columnName += s;
                        } else {
                            columnName += s.substring(0, 1).toUpperCase() + s.substring(1);
                        }
                        i ++;
                    }
                    SqlExecutor.execute(connection, "insert into e_generator_field(field_name,is_show,link_class,not_null,query,show_name,sort,sortable,type,class_id) values (?,?,?,?,?,?,?,?,?,?)",
                            columnName, isShow, className, !column.isNullable(), isQuery, column.getComment(), sort++,false, type, tableId);
                }
            }
        }
        DbUtil.close(connection);
    }

}
